/**
 * Created by godhh on 2016/12/20.
 */

import angular from 'angular'
import 'apiService'
import '@yunke/jquery_pagination'
import _ from 'lodash'
import 'angular-messages'
import AuthorizeController from '../../public/js/myservice/authorize'

// 申请出库
import './stock_out'

import '../../mypublic/css/common.less'
import '../../mypublic/css/index.less'
import '@yunke/img/process_center/page/css/process_center.less'
import '../css/process_center.less'

const app = angular.module('ProcessCenter', ['ApiService', 'ngMessages', 'stockOut']);

require('./delivery_order_ctrl');
require('./todo_ctrl');
require('./done_ctrl');

app.config(apiProvider => {

        const api = apiProvider.$get();

        api.setCamelCase(true);

        api.setUseFormData(false);

        api.setApis({
            fclist: {
                url: '/api/index.php?r=flow-center/list'
            },
            tenants: {
                url: '/orgapi/index.php?r=mtp/myscrm-organization/get-orgs'
            },
            contracts: {
                url: '/api/index.php?r=contract/search'
            },
            members: {
                url: '/api/index.php?r=erp-interface/get-users'
            },
            getApplications: {
                url: '/api/index.php?r=out-stock-order/get-application-list'
            },
            getProducts: {
                url: '/api/index.php?r=out-stock-order/get-product-list'
            },
            detail: {
                url: '/api/index.php?r=flow-center/detail'
            },
            getProjects: {
                url: '/api/index.php?r=out-stock-order/get-project-list'
            },
            getAreaCharges: {
                url: '/api/index.php?r=common/erp-user/get-area-charge-list'
            },
            getFinanceCharges: {
                url: '/api/index.php?r=common/erp-user/get-finance-charge-list'
            },
            getUser: {
                url: '/api/index.php?r=common/erp-user/get-user-by-id'
            },
            ch: {
                url: '/api/index.php?r=out-stock-flow/ch&orderId=',
                method: 'post'
            },
            kt: {
                url: '/api/index.php?r=out-stock-flow/kt&orderId=',
                method: 'post'
            },
            qr: {
                url: '/api/index.php?r=out-stock-flow/qr&orderId=',
                method: 'post'
            },
            sp: {
                url: '/api/index.php?r=out-stock-flow/sp&orderId=',
                method: 'post'
            },
            sq: {
                url: '/api/index.php?r=out-stock-flow/sq&orderId=',
                method: 'post'
            },
            fq: {
                url: '/api/index.php?r=out-stock-flow/fq&orderId=',
                method: 'post'
            },
            dh: {
                url: '/api/index.php?r=out-stock-flow/dh&orderId=',
                method: 'post'
            },
            getSteps: {
                url: '/api/index.php?r=out-stock-step/get-step-list&orderId='
            },
            save: {
                url: '/api/index.php?r=out-stock-order/save',
                method: 'post'
            },
            getCustomerInfo: {
                url: '/api/index.php?r=erp-interface/get-erp-customer-info'
            },
            getRDSDataInstanceList: {
                url: '/orgapi/index.php?r=mtp/myscrm-organization/get-rds-database-instance-list'
            },
            createTenant: {
                url: '/orgapi/index.php?r=mtp/myscrm-organization/create-organization',
                method: 'post'
            },
            createAuth: {
                url: '/orgapi/index.php?r=mtp/myscrm-organization/add-contract-auth',
                method: 'post'
            },
            getAreaList: {
                url: '/api/index.php?r=contract/get-contract-area-list&order_code='
            },
            getDeliveryOrders: {
                url: '/api/index.php?r=flow-center/delivery-order-list'
            },
            orderQr: {
                url: '/api/index.php?r=out-stock-flow/order-qr',
                method: 'post'
            },
            orderDh: {
                url: '/api/index.php?r=out-stock-flow/order-dh',
                method: 'post'
            },
            orderHt: {
                url: '/api/index.php?r=out-stock-flow/order-rollback',
                method: 'post'
            },
            getDeliveryTodos: {
                url: '/api/index.php?r=flow-center/delivery-todo-list'
            },
            getDeliveryDones: {
                url: '/api/index.php?r=flow-center/delivery-done-list'
            },
            getApproveRecord: {
                url: '/api/index.php?r=out-stock-flow/get-approve-record&sub_order_id='
            },
            view: {
                url: '/api/index.php?r=delivery-order/sub-order-view'
            },
            getAllPackages: {
                url: '/api/index.php?r=delivery-order/get-all-products&sub_order_id='
            }
        });

    })
    .filter('timeSplit', function () {

        return (origin, type = 'date') => {
            return origin.split(' ')[_.indexOf(['date', 'time'], type)];

        }

    })
    .constant('tabs', [
        {
            text: '出库单',
            type: 'deliveryOrder'
        }, {
            text: '我的待办',
            type: 'todo'
        }, {
            text: '我的已办',
            type: 'done'
        }
    ])
    .controller('ProcessCenterCtrl', ['$scope', 'tabs', function ($scope, tabs) {

        $scope.tabs = tabs;

        $scope.currentTab = $scope.tabs[0];

        $scope.onTabChange = function (tab) {

        };

        $scope.setToRoot = (name, value) => $scope[`_$_${name}`] = value;

        $scope.getFromRoot = (name) => {

            if ($scope[`_$_{name}`] !== undefined) {
                const result = _.cloneDeep($scope[`_$_{name}`]);
                $scope[`_$_{name}`] = undefined;
                return result;
            }

        };

        $scope.setTab = (type, name, value) => {

            if (name !== undefined) {
                $scope.setToRoot(name, value);
            }

            $scope.currentTab = _.find(tabs, x => x.type == type) || tabs[0];

        };


        const getTab = () => {

            const hash = window.location.hash || '';

            const matched = hash.match(/\w*=(\w*)[^&]/g) || [];

            const result = {};

            _.each(matched, match => {
                const kv = match.split('=');
                result[kv[0]] = kv[1];
            });

            return result;

        };

        if (getTab()['type']) {
            $scope.setTab(getTab()['type'])
        }

        AuthorizeController.getUserInfoPromise()
            .then(info => $scope.userInfo = info);

    }]);
